package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public Result addDish(DishDTO dto) {
        //保存菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        dishMapper.insert(dish);

        //保存菜品与品味的关系
        List<DishFlavor> flavors = dto.getFlavors();
        if (!CollectionUtils.isEmpty(flavors)) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
            dishFlavorMapper.batchInsert(flavors);
        }
        return Result.success();
    }

    @Override
    public Result queryDishesByPage(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());

        List<DishVO> dishVOList= dishMapper.selectByPage(dto);

        Page<DishVO> dishPage=(Page<DishVO>) dishVOList;

        long total = dishPage.getTotal();

        PageResult pageResult = new PageResult(total, dishVOList);
        return Result.success(pageResult);
    }

    @Override
    public Result deleteDishesByIds(List<Long> ids) {

        //1. 如果ids对应的菜品里，有未停售的菜品，则不能删除
        int enableDishCount = dishMapper.selectEnableDishCountByIds(ids);
        if (enableDishCount>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }

        //2. 如果ids对应的菜品里，有关联的套餐，则简单处理，直接不予删除
        int setmealCount = setmealDishMapper.selectSetmealCountByDishIds(ids);
        if (setmealCount > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品
        dishMapper.batchDeleteByIds(ids);
        //删除菜品对应的口味
        dishFlavorMapper.batchDeleteByDishIds(ids);

        return Result.success();
    }

    @Override
    public Result queryDishById(Long id) {
        Dish dish=dishMapper.selectById(id);

        List<DishFlavor> dishFlavors=dishFlavorMapper.selectByDishId(id);

        DishVO vo = new DishVO();
//        DishDTO vo = new DishDTO();
        BeanUtils.copyProperties(dish,vo);
        vo.setFlavors(dishFlavors);

        return Result.success(vo);
    }

    @Override
    @Transactional
    public Result updateDishById(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);
        dishMapper.updateById(dish);


        //口味根据主表的id进行删除
//        List<Object> list = new ArrayList<>();
//        list.add(dto.getId());
        dishFlavorMapper.batchDeleteByDishIds(Collections.singletonList(dto.getId()));

        //删除在新增
        List<DishFlavor> flavors = dto.getFlavors();
        if (!CollectionUtils.isEmpty(flavors)) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dish.getId()));
            dishFlavorMapper.batchInsert(flavors);
        }

        return Result.success();
    }
}
